﻿Public Class ViewData
    Inherits System.Web.UI.Page

    Protected Overrides Sub InitializeCulture()
        With ProjectSettings
            System.Threading.Thread.CurrentThread.CurrentUICulture = .Language
            System.Threading.Thread.CurrentThread.CurrentCulture = .Language
        End With

        MyBase.InitializeCulture()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            CheckSelectedData()
            RefreshDataset()
            lblInfo.Text = String.Format(lblInfo.Text, NumSelectedRecords)
        End If
    End Sub

    Private Function GetTopRows(dt As DataTable, RowCount As Integer) As DataTable
        Dim _table As DataTable
        _table = dt.Clone()
        For i As Integer = 0 To RowCount - 1
            If i >= dt.Rows.Count Then
                Exit For
            Else
                _table.ImportRow(dt.Rows(i))
            End If
        Next

        Return _table
    End Function

    Private Sub RefreshDataset()
        With gvData
            'set datasource for table
            'instead of using paging in control, revise sql to only pull the records I need (this only works with SQL Server)
            Dim dt As DataTable = SelectedDataTable
            If dt Is Nothing Then Exit Sub

            lblInfo.Text = String.Format(GetLocalResourceObject("lblInfoResource1.Text"), NumSelectedRecords)

            'it can take a little while to set the datasource if the number of records is large, so this could limit that...

            '.DataSource = GetTopRows(dt, 100)

            '...however, the number of records is limited on the Configure screen, so it is best just to show them all

            .DataSource = dt
            .AutoGenerateColumns = False

            With .Columns
                .Clear()
                .Add(New BoundField With {.DataField = "Station_ID", .HeaderText = GetGlobalResourceObject("WRDB", "Station_ID"), .SortExpression = "Station_ID"})
                .Add(New BoundField With {.DataField = "Date_Time", .HeaderText = GetGlobalResourceObject("WRDB", "Date_Time"), .SortExpression = "Date_Time", .HtmlEncode = False, .DataFormatString = "{0:g}"})
                .Add(New BoundField With {.DataField = "PCode", .HeaderText = GetGlobalResourceObject("WRDB", "PCode"), .SortExpression = "PCode"})
                .Add(New BoundField With {.DataField = "Depth_M", .HeaderText = GetGlobalResourceObject("WRDB", "Depth_M"), .SortExpression = "Depth_M"})
                .Add(New BoundField With {.DataField = "CCode", .HeaderText = "C", .SortExpression = "CCode"})
                .Add(New BoundField With {.DataField = "LCode", .HeaderText = "L", .SortExpression = "LCode"})
                .Add(New BoundField With {.DataField = "QCode", .HeaderText = "Q", .SortExpression = "QCode"})
                .Add(New BoundField With {.DataField = "RCode", .HeaderText = "R", .SortExpression = "RCode"})
                .Add(New BoundField With {.DataField = "Result", .HeaderText = GetGlobalResourceObject("WRDB", "Result"), .SortExpression = "Result", .DataFormatString = "{0:N3}"})
            End With

            'note: I had to comment out table settings in CSS for following to be used

            For i As Integer = 0 To 6
                Select Case i
                    Case 0, 2, 4 : .Columns(i).ItemStyle.HorizontalAlign = HorizontalAlign.Left
                    Case 3, 8 : .Columns(i).ItemStyle.HorizontalAlign = HorizontalAlign.Right
                    Case Else
                        .Columns(i).ItemStyle.HorizontalAlign = HorizontalAlign.Center
                End Select
                .Columns(i).HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            Next

            .DataBind()

        End With
    End Sub

    Private Sub gvData_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gvData.PageIndexChanging
        With CType(sender, GridView)
            .PageIndex = e.NewPageIndex
            RefreshDataset()
        End With
    End Sub

    Protected Sub gvData_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvData.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            For Each tc As TableCell In e.Row.Cells
                Dim l As New Label
                l.Height = 15
                l.Style.Add("Overflow-y", "hidden")
                l.Text = tc.Text
                l.ToolTip = tc.Text
                tc.Text = ""
                tc.Controls.Add(l)
            Next

            'format the Result column using the number of significant figures from the PCode table

            Dim PCode As String = TestNull(DataBinder.Eval(e.Row.DataItem, "PCode"), "")
            Dim Result As Double = TestNull(DataBinder.Eval(e.Row.DataItem, "Result"), 0.0)
            Dim rows() As DataRow = ProjectDataset.Tables("PCodes").Select(String.Format("PCode='{0}'", PCode))
            e.Row.Cells(8).HorizontalAlign = HorizontalAlign.Right
            If rows.Count = 1 Then
                Dim SigFigs As Integer = TestNull(rows(0).Item("Sig_Figs"), 2)
                e.Row.Cells(8).Text = String.Format("{0:N" & SigFigs & "}", Result)
            End If
        End If
    End Sub

End Class